<?php
class Task_warningcache extends Minion_Task {

	//预警缓存准备
	public function _execute(array $params) {
		
		set_time_limit ( 0 );
		ini_set('memory_limit','256M');
		$count_all = array();
		$echo = "";
		//获取缓存
		$result = Cache::instance()->get('skuinfo_');
		if(!empty($result)){
			$echo .= "#have data num".count($result);
			$echo .="#count now".date('Y-m-d h:i:s');//exit;
			//逐个计算
			
			for($i=0; $i<count($result);$i+=500){
				
				$result2 = array_slice($result,$i,500);
				foreach ($result2 as $sku_k =>$_result){
				
				$sku = $sku_k;
				$stocks = array();
				$date = date('Y-m',time());
				$date = $date.'-01';
				
				//期初库存
				$inventory = Cache::instance()->get($sku.'_inventory');
				if(empty($inventory)){
					$sql = "select quantity from inventories where inventory_date='".$date."' and sku='".trim($sku)."'";
					$inventory_temp = DB::query(Database::SELECT,$sql)->execute('newerp')->current();
					if(empty($inventory_temp)){
					
						$stocks['opening_num'] = 0;
					}else{
					
						$stocks['opening_num'] = $inventory_temp['quantity'];
					}
					Cache::instance()->set($sku.'_inventory', array(0=>$stocks['opening_num']), 3600*24);
				}else{
					$stocks['opening_num'] = $inventory[0];
				}
				
				//入库量
				$sel_wo = "SELECT sum(warehouse_quantity) as wo_num 
						FROM warehouseorders WHERE sku='".$sku."' AND warehouse_date >='".$date."'";
				$wo_num = DB::query(Database::SELECT,$sel_wo)->execute('newerp')->as_array();
				if(empty($wo_num[0]['wo_num'])){
					$stocks['wo_num'] =0;
				}else{
					$stocks['wo_num'] = $wo_num[0]['wo_num'];
				}
				//出库量
				$sel_do = "SELECT sum(quantity) as do_num 
						FROM deliveryorders WHERE sku='".$sku."'  AND delivery_date>= '".$date."'";
				$do_num = DB::query(Database::SELECT,$sel_do)->execute('newerp')->as_array();
				if(empty($do_num[0]['do_num'])){
					$stocks['do_num'] =0;
				}else{
					$stocks['do_num'] = $do_num[0]['do_num'];
				} 
				//实际库存量（所有已经仓库但还没离开仓库的库存）
				$stocks['actual_num'] = $stocks['opening_num']+$wo_num[0]['wo_num']-$do_num[0]['do_num'];
				
				//订单实际占用的库存量=总未发量
				$op_print_num = 0;
				$op_print_num = Cache::instance()->get($sku_k.'_erporderidnum');
				if(empty($op_print_num)){
					$echo .= '0';
				} 
			 	$stocks['op_print_num'] = $op_print_num;
				
				//总在途量
				//$stocks['pp_num'] = 0;
				$stocks['pp_num'] = Service_Warningcount::get_inventory_onway($sku);
				
				//实际库存可用量=实际库存量-订单总未发量
				$stocks['actual_available_num'] = $stocks['actual_num'] - $stocks['op_print_num'];
				
				//总可用量
				$stocks['all_available_num'] = $stocks['actual_available_num']+$stocks['pp_num'];
				
				//库存信息
				$_result['inventory']= $stocks;
				
				//可支持天数
				$_result['supp_days'] = (float)$stocks['all_available_num']/(float)$_result['daysales'] - (float)$_result['wave_days'];
				
				//最佳下单日期差值
				$_result['best'] = $_result['supp_days']- intval($_result['delivery']); 
				
				//全部需要预警的sku
				if($_result['best']<=7)
				{
					$count_all[$sku_k] = $_result;
				}
				
			}
			}
		}
			//缓存计算的值
			Cache::instance()->delete('result_');
			Cache::instance()->set('result_',$count_all,60*60*12);
			$echo .= "#count all".count($count_all);
			$echo .="#end".date("Y-m-d h:i:s");
			echo $echo."\n";
	}
}